#! encoding = utf-8

import serial.tools.list_ports as serailListPort
from threading import Thread
from ArmlogController import ArmLogController
import time, os, sys


class serialLinster(Thread):

    def __init__(self, port, baudrate, commandObj):
        super(serialLinster, self).__init__()
        self.com = "COM{}".format(port)
        self.baudrate = baudrate
        self.assertApFlag = False
        self.restartFlag = False
        self.online = False
        self.Obj = commandObj
        self.path = os.path.split(os.path.abspath(sys.argv[0]))[0]
        self.assertLogDir = os.path.join(self.path, 'assertApLog')

        if not os.path.exists(self.assertLogDir):
            os.mkdir(self.assertLogDir)

    def run(self):
        vid = self.getVidByComName(self.com)
        while True:
            self.checkDiagIsRestart()
            flag, u2sDiag = self.getU2SDiagFlag(vid)
            if flag:
                self.assertApFlag = True
                self.catcherLogelWhenAssertAP(u2sDiag)
                self.assertApFlag = False

            if self.restartFlag:
                self.restartOnline()
            time.sleep(1)

    def catcherLogelWhenAssertAP(self, u2sDiagPort):

        armLogControllerObj = ArmLogController(u2sDiagPort, self.path, self.assertLogDir)
        armLogControllerObj.startArmlog()
        time.sleep(5 * 60)
        bt = time.time()
        while True:
            et = time.time()
            ht = (et - bt) / 60
            if armLogControllerObj.checkAssertIsEnd() or ht > 20:
                armLogControllerObj.closedArmLog()
                time.sleep(3)
                armLogControllerObj.moveLog()
                break

            time.sleep(5*60)


    def getAllComInfo(self):
        return serailListPort.comports()

    def getVidByComName(self, com_port="COM1"):
        for portInfo in self.getAllComInfo():
            if portInfo.device.lower() == com_port.lower():
                return portInfo.vid

    def getU2SDiagFlag(self, vid):
        for portInfo in self.getAllComInfo():
            if portInfo.description.lower().find(u"sprd u2s diag") != -1 and vid == portInfo.vid:
                return True, portInfo.device
        return False, None

    def checkDiagIsRestart(self):
        com_list = []
        for portInfo in self.getAllComInfo():
            com_list.append(portInfo.device.lower())

        if self.com.lower() not in com_list:
            self.restartFlag = True

    def checkDiagIsOnline(self):
        for portInfo in self.getAllComInfo():
            if portInfo.device.lower() == self.com.lower():
                self.online = True
                return
        self.online = False

    def restartOnline(self):
        self.checkDiagIsOnline()
        if self.online == True:
            # self.Obj.SP_BeginPhoneTest(int(str(port)[3:]) if "com" in str(port).lower() else int(port), self.baudrate)
            self.Obj.beginPhoneTest()
            self.restartFlag = False

    def getAssertApFlag(self):
        return self.assertApFlag